<?php
/* -----------------------------------------------------------------
 *                        Aveghe My Blog
 * -----------------------------------------------------------------
 * Aveghe My Blog: made your blog easy!
 * Copyright (C) 2008 Elia Contini
 * 
 * Aveghe My Blog is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Aveghe My Blog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

class User
	{
		public function login($username, $password)
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$query = "SELECT * FROM ".$tablePrefix."users WHERE user_login=\"".$username."\" AND user_password=\"".$password."\"";
				$row = $db->fetchRow($query);
				
				return $row;
			}
			
		public function getUserById($userId)
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$query = "SELECT * FROM ".$tablePrefix."users WHERE user_id=". (int) $userId;
				
				$row = $db->fetchRow($query);
				return $row;
			}
		
		public function getUserByLogin($userLogin)
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$query = "SELECT * FROM ".$tablePrefix."users WHERE user_login=\"".$userLogin."\"";
				
				$row = $db->fetchRow($query);
				return $row;
			}
		
		public function updateAvatar( $userId, $userAvatar)
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$data = array( 
							   'user_avatar' => $userAvatar
							 );
				$where = "user_id = " . (int)$userId;
				return $db->update($tablePrefix.'users', $data, $where);
			}
		
		public function updateUserInfo( $userId,
									  	$userName,
									  	$userSurname,
									  	$userEmail,
									  	$userJabber
									  )
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$data = array( 
							   'user_name' => $userName,
							   'user_surname' => $userSurname,
							   'user_email' => $userEmail,
							   'user_jabber' => $userJabber
							 );
				$where = "user_id = " . (int)$userId;
				return $db->update($tablePrefix.'users', $data, $where);
			}

		public function updateUserDescription($userId, $userDescription)
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$data = array( 
							   'user_description' => $userDescription,
							 );
				$where = "user_id = " . (int)$userId;
				return $db->update($tablePrefix.'users', $data, $where);
			}
		
		public function updatePassword($userId, $userPassword)
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$data = array( 
							   'user_password' => $userPassword,
							 );
				$where = "user_id = " . (int)$userId;
				return $db->update($tablePrefix.'users', $data, $where);
			}
		
		public function getAllUsers()
			{
				$db = Zend_Registry::get('db');
				$tablePrefix = Zend_Registry::get('tablePrefix');
				
				$query = "SELECT * FROM ".$tablePrefix."users";
				
				$rows = $db->fetchAll($query);
				return $rows;
			}
	}
?>